<?php

namespace Controle\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Controle\ControleBundle\Entity\Theme;
use Controle\ControleBundle\Form\ThemeType;
use Controle\ControleBundle\Entity\MotCle;
use Controle\ControleBundle\Form\MotCleType;

class ThemeController extends Controller {

    public function newAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        if ($id != '')
            $theme = $em->getRepository('ControleBundle:Theme')->find($id);
        else
            $theme = new Theme();
        $form = $this->createForm(new ThemeType(), $theme);
        if ($request->isMethod('post')) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                $theme = $form->getData();
                $em->persist($theme);
                $mots = $request->get('mots');
                $mots = json_decode($mots);
                foreach ($mots as $mot) {
                    $mot = $mot->value;
                    $m = $em->getRepository('ControleBundle:MotCle')->findOneBy(array('libelle' => $mot));
                    if (!$m) {
                        $m = new MotCle();
                        $m->setLibelle($mot);
                        $em->persist($m);
                    }
                    $m->addTheme($theme);
                    $theme->addMotCle($m);
                }
                $em->flush();
                $url = $this->generateUrl('admin_theme_list', array('type' => 'ajax'));
                return $this->redirect($url);
            }
        }
        return $this->render('AdminBundle:Theme:new.html.twig', array('form' => $form->createView()));
    }

    public function deleteAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $theme = $em->getRepository('ControleBundle:Theme')->find($id);
        $em->remove($theme);
        $em->flush();
        $url = $this->generateUrl('admin_theme_list', array('type' => 'ajax'));
        return $this->redirect($url);
    }

    public function listAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $themes = $em->getRepository('ControleBundle:Theme')->findAll();
        if ($request->get('type') == 'ajax')
            return $this->render('AdminBundle:Theme:page_list.html.twig', array('themes' => $themes));
        return $this->render('AdminBundle:Theme:list.html.twig', array('themes' => $themes));
    }

    public function getMotsClesAction() {
        $request = $this->getRequest();
        $mot = $request->get('mot');
        $em = $this->getDoctrine()->getManager();
        $mots = $em->getRepository('ControleBundle:MotCle')->getMotCleAutocomplate($mot);
        $tab = array();

        foreach ($mots as $m) {
            $t = array();
            $t['id'] = $m->getId();
            $t['value'] = $m->getLibelle();
            $tab[] = $t;
        }
        $result = array('mots' => $tab);
        return new \Symfony\Component\HttpFoundation\Response(json_encode($result));
    }

}
